unsigned short int* getFlashInfo(void)
{
	return(0); //For now	
}

unsigned short int flashReadSegment(unsigned short int segment, unsigned short int offset)
{
	offset = offset * 2;
	
	switch (segment)
	{
		case 0:
			return(flashRead(offset));
		
		case 1:
			return(flashRead(64 + offset));
		
		case 2:
			return(flashRead(128 + offset));
			
		case 3:
			return(flashRead(192 + offset));
		
		default:
			return(0);
	}
	
}

unsigned short int flashDumpSegment(unsigned short int segment, unsigned short int* buffer)
{
	register unsigned short int count = 0;
	
	auto unsigned short int offset = 0;
	
	switch (segment)
	{
		case 'A':
			//offset = 0;
			break;
		
		case 'B':
			offset = 64;
			break;
			
		case 'C':
			offset = 128;
			break;
		
		case 'D':
			offset = 192;
			break;
		
		default:
			return(1);
	}
	
	while(count != 32)
	{
		*buffer = flashRead(0x1000 + offset + count);
		buffer = buffer + 2;
		count = count + 2;
	}
	
	return(0);
}

unsigned short int flashRead(unsigned short int offset)
{
	auto unsigned short int* flashaddress = (unsigned short int*)0x1000 + offset;
	return(*flashaddress);
}

unsigned short int flashWrite(unsigned short int* buffer, unsigned short int segment)
{
	FCTL1 = FWKEY + WRT;
	return(0);
}

unsigned short int flashAppend(unsigned short int* buffer, unsigned short int segment)
{
	return(0);
}

unsigned short int flashErase(unsigned short int segment, unsigned short int key)
{
	WDTCTL = WDTPW + WDTHOLD;
	setCPUFreq(M1);
	auto unsigned short int oldcpuspeed = getCPUFreq();
	
	auto unsigned short int* flashmempointer;
	
	switch (segment)
	{
		case 1:
		return(4);
		
		case 2:	
			flashmempointer = (unsigned short int*)0x1000 + 32;
		break;
		
		case 3:
			flashmempointer = (unsigned short int*)0x1000 + 64;
		break;
			
		case 4:
			flashmempointer = (unsigned short int*)0x1000 + 96;
		break;
		
		default:
			return(5);
	}	
	
	FCTL1 = FWKEY + ERASE;
	FCTL2 = FWKEY + (FSSEL_1 | 3);
	FCTL3 = FWKEY + 0;
	
	//Start erase
	*flashmempointer = 0xFFFF;
	
	FCTL3 = FWKEY + LOCK;
	FCTL2 = FWKEY + 0;
	FCTL1 = FWKEY + 0; 
	
	if(setCPUFreq(oldcpuspeed) != 0)
	{
		return(2);
	}
	
	if(*flashmempointer != 0 || (unsigned char)(FCTL3 & FAIL) > 0)
	{
		return(1); //Failure
	}
	else
	{
		return(0); //Flash Erase was a success!
	}
}
